﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Model01.Modifier;

namespace StreetLight
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Streetlight streetlight = new Streetlight(1000, 0, 0, new Model01.Objects.Size(1, 1));
            streetlight.OnLightChange += new OnLihgtChangeEvent(streetlight_OnLightChange);
            streetlight.StartFromGreen();
            this.BackColor = Color.Gray;
            PaintStreetlight(this.CreateGraphics());
        }

        void streetlight_OnLightChange(object sender, LihgtChangeEventArgs e)
        {
            switch (e.Light)
            {
                case Lights.Green:
                    {
                        Colors.MainColor = Color.FromArgb(0, 255, 0);                        
                        Colors.Light = Color.FromArgb(130, 255, 130);
                    }
                    break;
                case Lights.Yellow:
                    {
                        Colors.MainColor = Color.FromArgb(255, 200, 0);
                        Colors.Light = Color.FromArgb(255, 225, 130);
                    }
                    break;
                case Lights.Red:
                    {
                        Colors.MainColor = Color.FromArgb(255, 0, 0);
                        Colors.Light = Color.FromArgb(255, 130, 130);
                    }
                    break;
            }
            Colors.BorderOut = Color.FromArgb(20, Colors.MainColor);
            Colors.BorderOutCenter = Color.FromArgb(40, Colors.MainColor);
            Colors.BorderCenter = Color.FromArgb(60, Colors.MainColor);
            Colors.BorderInCenter = Color.FromArgb(80, Colors.MainColor);
            Colors.BorderIn = Color.FromArgb(100, Colors.MainColor);
            VoidDel d = () => this.Refresh();
            try
            {
                Monitor.Enter(this);
                if (this.InvokeRequired)
                    this.Invoke(d);
            }
            finally
            {
                Monitor.Exit(this);
            }
        }

        public void PaintStreetlight(Graphics g)
        {
            int x = 100, y = 100, N = 10;
            g.FillEllipse(new SolidBrush(Colors.MainColor), x - 6, y - 1, 11, 11);
            g.FillEllipse(new SolidBrush(Colors.Light), x - 4, y + 1, 7, 7);
            g.FillRectangle(new SolidBrush(Color.White), x - 1, y + 4, 2, 2);
            g.FillRectangle(new SolidBrush(Color.Black), x, x, N, N);
            g.DrawArc(new Pen(Colors.BorderOut, 1), x - 11, y - 6, 21, 21, 0, 360);
            g.DrawArc(new Pen(Colors.BorderOutCenter, 2), x - 10, y - 5, 19, 19, 0, 360);
            g.DrawArc(new Pen(Colors.BorderCenter, 2), x - 9, y - 4, 17, 17, 0, 360);
            g.DrawArc(new Pen(Colors.BorderInCenter, 1), x - 8, y - 3, 15, 15, 0, 360);
            g.DrawArc(new Pen(Colors.BorderIn, 2), x - 7, y - 2, 13, 13, 0, 360);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            PaintStreetlight(e.Graphics);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BackColor = Color.LightSkyBlue;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.BackColor = Color.Lavender;
        }
    }

    public delegate void VoidDel();

    public struct Colors
    {
        public static Color MainColor;
        public static Color BorderOut;
        public static Color BorderOutCenter;
        public static Color BorderCenter;
        public static Color BorderInCenter;
        public static Color BorderIn;
        public static Color Light;
    }
}
